<?php
	require_once("include/jara_fns.php");
	header("Content-Type: application/rss+xml; charset=utf-8");
	echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n";
	$title = "";
	$link = "";
	$result = false;
	$summary = isset($_REQUEST["summary"]);
	if(!get_magic_quotes_gpc()) {
		if(isset($_REQUEST["category"])) {
			$_REQUEST["category"] = addslashes($_REQUEST["category"]);
		}
		else if(isset($_REQUEST["term"])) {
			$_REQUEST["term"] = addslashes($_REQUEST["term"]);
		}
	}
	if(isset($_REQUEST["category"])) {
		$result = jara_db_query("select * from jara_posts where categoryid = '".$_REQUEST["category"]."' order by time desc limit 20");
		$category = jara_get_category($_REQUEST["category"]);
		$title = $jconfig->settings["blog_title"] . " - " . $category["title"];
		$link = jara_category_permalink($category["categoryid"]);
	}
	else if(isset($_REQUEST["term"])) {
		$result = jara_db_query("select * from jara_posts where title like '%".$_REQUEST["term"]."%' or text like '%".$_REQUEST["term"]."%' order by time desc limit 20");
		$title = $jconfig->settings["blog_title"] . " - " . $_REQUEST["term"];
		$link = $jconfig->settings["blog_url"] . "/search.php?term=".$_REQUEST["term"];
	}
	else {
		$result = jara_db_query("select * from jara_posts order by time desc limit 20");
		$title = $jconfig->settings["blog_title"];
		$link = $jconfig->settings["blog_url"] . "/index.php";
	}
?>
<rss version="2.0">
	<channel>
		<title><?php echo $title; ?></title>
		<link><?php echo $link; ?></link>
		<?php
			for($i = 0; $i < $result->num_rows; $i++) {
				$row = $result->fetch_assoc();
				$title = $row["title"];
				$link = jara_permalink($row["id"], "url");
				if($summary) {
					$desc = "<![CDATA[".((strlen($row["text"]) > 500) ? (substr($row["text"], 0, 500) . " ... ") : ($row["text"])) . "]]>";
				}
				else {
					$desc = "<![CDATA[".$row["text"]."]]>";
				}
				$pubDate = date("r", $row["time"]);
		?>
		<item>
			<title><?php echo $title; ?></title>
			<link><?php echo $link; ?></link>
			<pubDate><?php echo $pubDate; ?></pubDate>
			<description><?php echo $desc; ?></description>
		</item>
		<?php
			}
		?>
	</channel>
</rss>